import { useMessage } from "@/hooks"

export function useVariableRemove(panelJson: TPanel) {
  // 删除变量
  function remove(item: IPageData, index: number) {
    if(isVariable(item.variable)) return
    panelJson.data.splice(index, 1)
    useMessage().success('删除成功')
  }

  // 页面是否引用了该变量
  function isVariable(field: string) {
    for(let hook of panelJson.hooks) {
      const { kind, inters, pars } = hook
      if(kind === '0') {
        for(let { params, body, heads, rets } of inters) {
          const isUse1 = isInterParams(field, params)
          if(isUse1) return referenceTips()
          const isUse2 = isInterParams(field, body)
          if(isUse2) return referenceTips()
          const isUse3 = isInterParams(field, heads)
          if(isUse3) return referenceTips()
          const isUse4 = isInterRets(field, rets)
          if(isUse4) return referenceTips()
        }
      } else if(kind === '3') {
        const isUse = isPars(field, pars)
        if(isUse) return referenceTips()
      }
    }
    for(let item of panelJson.children) {
      const { attrs, events } = item
      const vForAttr = attrs.find(item => item.name === 'vFor')
      if(vForAttr?.value.open) {
        if(vForAttr?.value.variable === field) {
          return referenceTips()
        }
      } else {
        const vModelAttr = attrs.find(item => item.name === 'vModel')
        if(vModelAttr?.value === field) {
          return referenceTips()
        }
      }
      const { list } = events
      for(let event of list) {
        const { kind, inters, pageJump } = event
        if(kind === '0') {
          for(let { params, body, heads, rets } of inters) {
            const isUse1 = isInterParams(field, params)
            if(isUse1) return referenceTips()
            const isUse2 = isInterParams(field, body)
            if(isUse2) return referenceTips()
            const isUse3 = isInterParams(field, heads)
            if(isUse3) return referenceTips()
            const isUse4 = isInterRets(field, rets)
            if(isUse4) return referenceTips()
          }
        } else if(kind === '1') {
          const isUse = isInterParams(field, pageJump.params)
          if(isUse) return referenceTips()
        }
      }
    }
    return false
  }

  function referenceTips() {
    useMessage().warning('该变量已引用，无法删除')
    return true
  }

  function isInterParams(field: string, params: TParamItem[]) {
    let isUse = false
    for(let param of params) {
      if(param.type === '1' && param.value === field) {
        isUse = true
        break
      }
    }
    return isUse
  }

  function isInterRets(field: string, rets: TParamItem[]) {
    let isUse = false
    for(let ret of rets) {
      if(ret.type === '0' && ret.value === field) {
        isUse = true
        break
      }
    }
    return isUse
  }

  function isPars(field: string, pars: Array<Omit<TParamItem, 'type'>>) {
    let isUse = false
    for(let par of pars) {
      if(par.value === field) {
        isUse = true
        break
      }
    }
    return isUse
  }
  return { remove }
}